﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace SkypeBot.Commands
{
    using SkypeBot.Core;

    public class DefaultCommandMapper : ICommandMapper
    {

        private readonly Regex _regex;
        public DefaultCommandMapper(IMessageHandler handler, MethodInfo endPoint, string pattern)
        {
            _regex = new Regex(pattern, RegexOptions.IgnoreCase);
            Handler = handler;
            EndPoint = endPoint;
        }

        public IMessageHandler Handler { get; private set; }

        public MethodInfo EndPoint { get; private set; }

        public bool IsMatch(Message message)
        {
            return !string.IsNullOrEmpty(message.MessageContent) && _regex.IsMatch(message.MessageContent);
        }

        public Dictionary<string, string> GetInputParameters(Message message)
        {
            Match match = _regex.Match(message.MessageContent);
            return _regex.GetGroupNames().ToDictionary(name => name, name => match.Groups[name].Value.Trim());
        }
    }
}
